package com.metro.paperless.common.utils.resource;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 系统资源
 * 
 * @author xk
 * @2013-4-19
 */
public abstract class Res {
	private static SqlSessionTemplate sqlSessionTemplate;
	private static JdbcTemplate jdbcTemplate;
	
	/** spring applicationContext */
	private static ApplicationContext applicationContext;

	//数据库资源文件
	private static Properties dbProperties;
	
	//系统资源文件
	private static Properties sysProperties;

    //缓存
    private static CacheManager cacheManager;

	/**
	 * 取得spring JdbcTemplate的引用
	 * @see JdbcTemplate
	 * @return
	 */
	public static JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public static void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		Res.jdbcTemplate = jdbcTemplate;
	}

	public static SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public static void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		Res.sqlSessionTemplate = sqlSessionTemplate;
	}

	public static Properties getProperties() {
		if(dbProperties != null){
			for (Object object : sysProperties.keySet()) { 
				if(!dbProperties.containsKey(object.toString())){
					dbProperties.put(object.toString(), sysProperties.getProperty(object.toString()).toString());
				}
			}
			return dbProperties;
		}
		return sysProperties;
	}

	public static void setDbProperties(Properties properties) {
		Res.dbProperties = properties;
	}
	
	public static void setProperties(Properties properties) {
	    Res.sysProperties = properties;
	}

	/**
	 * 获取系统配置文件中的属性值
	 * @param key
	 * @return 如果不存在就返回null
	 */
	public static String getPropertiy(String key){
		if(dbProperties == null || dbProperties.get(key) == null){
			return sysProperties.getProperty(key);
		}
		return dbProperties.getProperty(key);
		
	}
	
	public static int getPropertyInt(String key) {
		String tmp = dbProperties.getProperty(key);
		if (StringUtils.isNotEmpty(tmp)) {
			return Integer.parseInt(tmp.trim());
		}
		return 0;

	}

	public static int getPropertyInt(String key, int defaultValue) {
		String tmp = dbProperties.getProperty(key);
		if (StringUtils.isNotEmpty(tmp)) {
			return Integer.parseInt(tmp.trim());
		}
		return defaultValue;
	}
	
	/**
	 * 获取系统配置文件中的属性值
	 * @param key
	 * @param defaultVal
	 * @return 如果不存在就返回默认值
	 */
	public static String getPropertiy(String key,String defaultVal){
		String val = null;
		if(dbProperties == null || dbProperties.getProperty(key) == null){
			val = sysProperties.getProperty(key);
			return val == null ? defaultVal : val;
		}
		return defaultVal;
	}

    public static Cache getCache(String cacheName) {
        return cacheManager.getCache(cacheName);
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        Res.applicationContext = applicationContext;
    }

    public static CacheManager getCacheManager() {
        return cacheManager;
    }

    public static void setCacheManager(CacheManager cacheManager) {
        Res.cacheManager = cacheManager;
    }

}
